#include "../include/server.h"

int  server::serverinit()
{
#ifdef _WIN32
    WSADATA wsa;
    if(WSAStartup(0x0202,&wsa) == -1)
    {
        return -1;
    }
    else
    {
        return 1;
    }

#endif
#ifdef linux
    return 1;
#endif
}
//lay para can thiet
int server::servercreatesocket()
{
    slisten = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
    if(listen <= 0 )
    {
        return -1;

    }
    else
    {
        return 1;
    }

}
int server::serverlisten()
{

    if(listen(slisten,1) <0)
    {
        return -1;
    }
    else
    {
        return 1;
    }
}
int server::serveraccept()
{

    socklen_t len = sizeof(client);
    cl = accept(slisten,(sockaddr*)&client,&len);
    if(cl <= 0)
    {
        return -1;
    }
    else
    {
        return 1;
    }

}
int server::serverbind()
{
    this->port = 9999;
    serv.sin_family = AF_INET;
    serv.sin_port = htons(port);
    serv.sin_addr.s_addr = inet_addr("127.0.0.1");
    if(bind(slisten,(sockaddr*)&serv,sizeof(serv)) < 0 )
    {
        return -1;
    }
    else
    {
        return 1;
    }

}
int server::serversend()
{

    int size = send(cl,(char *)&sen,sizeof(sen),0);
    return size;

}

int server::serverrecv()
{
    int size =  recv(cl,(char*)&rec,sizeof(rec),0);
    return size;
}
void server::servercleanup()
{
#ifdef _WIN32
    WSACleanup();
#endif
}
void server::serversocketclose()
{
#ifdef _WIN32
    closesocket(slisten);
    servercleanup();
#endif
#ifdef linux
    close(slisten);
#endif

}
void server::serverclose()
{
#ifdef _WIN32

    closesocket(slisten);
    closesocket(cl);
    servercleanup();
#endif
#ifdef linux
    close(slisten);
    close(cl);
#endif
}
server::server()
{
    this->slisten = -1;
    this->port = -1;
    this->cl = -1;
}
server::~server()
{
    this->slisten = -1;
    this->port = -1;
    this->cl = -1;
}
